<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use app\common\controller\Login;
use think\Request;
class Circle extends Login
{
    public function index()
    {
        $num = session::get('admin');

        $user = db('user')->where('id',$num)->find();

        $agent_id  = $user['agent_id'];

        $data = Db::table('business_circle')->where('agent_id',$agent_id)->select();

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function create()
    {
        return $this->fetch();
    }

    //增加商圈逻辑
    public function store()
    {
        $num = session::get('admin');

        $user = db('user')->where('id',$num)->find();

        $agent_id  = $user['agent_id'];

        $_POST['agent_id'] = $agent_id;

        Db::table('business_circle')->insert($_POST);

        return redirect('index');
    }

    public function delete($id)
    {
        Db::table('business_circle')->where('id',$id)->delete();

        return redirect('index');
    }

    public function search()
    {
        $like = $_POST['like']; //关键字

        $con['circle_name']= array('like',"%$like%");

        $data=Db::table('business_circle')->where($con)->select();

        $this->assign('data',$data);

        return $this->fetch('index');
    }

    public function edit($id)
    {
        $data = Db::table('business_circle')->where('id',$id)->find();

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function update($id)
    {
        $data = Db::table('business_circle')->where('id',$id)->update($_POST);

        return redirect('index');
    }
}